package com.example.sisko_database;

import java.io.IOException;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DatabaseUpdate {
	
	
	private DatabaseInit myDbHelper;
	private SQLiteDatabase database;
	
	public DatabaseUpdate(Context context) {
		myDbHelper = new DatabaseInit(context);
	
		try {
		  
			myDbHelper.createDataBase();
		  
		} catch (IOException ioe) {
		
			throw new Error("Unable to create database");
		  
		}
		  
		try {
		  
			database = myDbHelper.openDataBase();
		 
		}catch(SQLException sqle){
		  
			Log.w("HALTSTOP", "FEHLER21");
			throw sqle;
			 
		}
	}
	
	//Close DB
	public void close(){
		database.close();
	}
	
	//Updates the favorite column - selects/deselects items as favorite
	public void updateFavorit(int rating_id, int favorit){
		
		ContentValues value = new ContentValues();
		
		value.put("favorit", String.valueOf(favorit));
		database.update("TABLE_RATING", value, "rating_id" + "=" + "\""+String.valueOf(rating_id)+"\"",null);
		
	}
	
	//Updates the rating_bar column (sets the amount of stars)
	public void updateRating(String rating_id, float rating){
		
		ContentValues value = new ContentValues();
		value.put("rating_bar", rating);
		database.update("TABLE_RATING", value, "rating_id="+"\""+rating_id+"\"", null);
		
	}
}
